﻿using System;
using System.Data;
using System.Text;
using YookingSvrDBCall.Class.DBPub;
using YookingSvrDBCall.Class.DBLogic;

namespace YookingBizWQW.Class.Credit.pub
{
    public class clsMbrPayNotify
    {
        //注释：王庆文 2020年05月12日=================================================================================
        //用户通过APP进行充值操作，根据支付平台异步反馈过来的消息，修改充值状态为成功。
        //然后再根据用户相应的充值金额，将会员升级到对应的级别，并对其上级、上上级会员进行充值奖励。
        public string PayNotify_HandleCreditBiz(string strAppID, string strOrderID)
        {
            string strBizStatus = "success"; //默认业务处理为成功

            //先查找充值未成功的资金记录
            DataSet dsPool = GetNoSuccess_RechargePoolData(strAppID, strOrderID); 
            if (dsPool != null && dsPool.Tables.Count > 0 && dsPool.Tables[0].Rows.Count > 0)
            {
                DataTable dtPool = dsPool.Tables[0];
                foreach (DataRow drRow in dtPool.Rows)
                {
                    try
                    {
                        #region 解析出充值未成功的字段名及字段值，后续逻辑会用到

                        string strUnitCode = drRow["UNITCODE"].ToString();           //平台编码
                        string strPayPerson = drRow["PAYPERSON"].ToString();         //充值人员
                        string strPayTime = drRow["PAYTIME"].ToString();             //充值时间
                        string strPayMode = drRow["PAYMODE"].ToString();             //充值方式
                        string strPayAmount = drRow["PAYAMOUNT"].ToString();         //充值金额
                        //string strOrderID = drRow["ORDERID"].ToString();           //订单号码
                        string strIsSuccess = drRow["ISSUCCESS"].ToString();         //是否支付成功
                        string strPartnerID = drRow["PARTNERID"].ToString();         //收款商家ID
                        //string strAppID = drRow["APPID"].ToString();               //应用APPID
                        string strWxOpenID = drRow["WXOPENID"].ToString();           //微信用户标识
                        string strWxTradeState = drRow["WXTRADESTATE"].ToString();   //微信交易状态
                        string strWxOrderID = drRow["WXORDERID"].ToString();         //微信支付订单号
                        string strWxTimeEnd = drRow["WXTIMEEND"].ToString();         //微信支付完成时间
                        string strAccountBank = drRow["ACCOUNTBANK"].ToString();     //开户银行
                        string strAccountName = drRow["ACCOUNTNAME"].ToString();     //银行户名
                        string strAccountNumber = drRow["ACCOUNTNUMBER"].ToString(); //银行账号
                        string strMbrUpdLevel = drRow["MBRUPDLEVEL"].ToString();     //升级级别
                        string strMbrUpdWay = drRow["MBRUPDWAY"].ToString();         //升级方式
                        string strOtherMemo = drRow["OTHERMEMO"].ToString();         //其他备注
                        string strSys_RecordID = drRow["SYS_RECORDID"].ToString();   //记录ID

                        #endregion

                        #region 将该条记录的充值状态修改为“已成功”

                        StringBuilder sbUpdateSQL = new StringBuilder();
                        sbUpdateSQL.AppendLine(" UPDATE XYK_RECHARGEPOOL SET ISSUCCESS='1' ");
                        sbUpdateSQL.AppendLine(" WHERE SYS_RECORDID='" + strSys_RecordID + "' ");

                        clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
                        object objSucCount = myDB.YookingExecuteNonQuery("Data", sbUpdateSQL.ToString());

                        #endregion

                        if (Convert.ToInt32(objSucCount) > 0) //如果充值成功
                        {
                            clsMbrUpdate myMUpd = new clsMbrUpdate(); //升级会员级别，为该会员的上级、上上级支付升级奖励
                            object objUpdCount = myMUpd.ChangeMemberLevel(strUnitCode, strPayPerson, strMbrUpdLevel, strMbrUpdWay);

                            //会员升级、上级、上上级奖励成功
                            if (Convert.ToInt32(objUpdCount) > 0)
                            {
                                strBizStatus = "success"; //最终成功了
                            }
                            else
                            {
                                strBizStatus = "fail"; //最终失败了 
                            }
                        }
                    }
                    catch { }
                }
            }

            return strBizStatus; //最终的业务处理结果状态
        }

        //注释：王庆文 2020年05月12日==========================================================
        //按照指定条件，查询支付未成功的资金充值记录数据
        private DataSet GetNoSuccess_RechargePoolData(string strAppID, string strOrderID)
        {
            #region 生成查询SQL语句

            StringBuilder sbQuerySQL = new StringBuilder();
            sbQuerySQL.AppendLine(" SELECT ");
            sbQuerySQL.AppendLine("     UNITCODE, ");
            sbQuerySQL.AppendLine("     PAYPERSON, ");
            sbQuerySQL.AppendLine("     PAYTIME, ");
            sbQuerySQL.AppendLine("     PAYMODE, ");
            sbQuerySQL.AppendLine("     PAYAMOUNT, ");
            sbQuerySQL.AppendLine("     ORDERID, ");
            sbQuerySQL.AppendLine("     ISSUCCESS, ");
            sbQuerySQL.AppendLine("     PARTNERID, ");
            sbQuerySQL.AppendLine("     APPID, ");
            sbQuerySQL.AppendLine("     WXOPENID, ");
            sbQuerySQL.AppendLine("     WXTRADESTATE, ");
            sbQuerySQL.AppendLine("     WXORDERID, ");
            sbQuerySQL.AppendLine("     WXTIMEEND, ");
            sbQuerySQL.AppendLine("     ACCOUNTBANK, ");
            sbQuerySQL.AppendLine("     ACCOUNTNAME, ");
            sbQuerySQL.AppendLine("     ACCOUNTNUMBER, ");
            sbQuerySQL.AppendLine("     MBRUPDLEVEL, ");
            sbQuerySQL.AppendLine("     MBRUPDWAY, ");
            sbQuerySQL.AppendLine("     OTHERMEMO, ");
            sbQuerySQL.AppendLine("     SYS_RECORDID ");
            sbQuerySQL.AppendLine(" FROM XYK_RECHARGEPOOL T ");
            sbQuerySQL.AppendLine(" WHERE " + clsSQLSyntax.IsNullSQL("Data", "T.SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine(" AND T.APPID = '" + strAppID + "' ");
            sbQuerySQL.AppendLine(" AND T.ORDERID = '" + strOrderID + "' ");
            sbQuerySQL.AppendLine(" AND T.ISSUCCESS = '0' ");

            #endregion

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            return myDB.YookingExecuteDataSet("Data", sbQuerySQL.ToString());
        }



    }
}
